<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;

use app\admin\model\ComValidate;
use app\admin\model\ComModel;

#消息管理#
class Msg extends Common{

	public function _initialize(){
		if(request()->isGet()){

			$action= Request::instance()->action();
			$controller=strtolower( Request::instance()->controller());

			switch ($action) {
				case 'newslist':
					$this->assign('title',"留言列表");
					break;
				
			}

			$this->assign('controller',$controller);
			
		}
	}
	#列表#
	public function msglist(){

		if(request()->isGet()){

			$this->log("查看留言列表");
			
			return $this->fetch();
		}

	}

	public function jsonmsglist(){

			$map=[];

			$page=input('page');
			$limit=input('limit');
			$keyword=input('keyword');
			$status=input('status');
			$start=input('start');
			$end=input('end');
				
			if($keyword){
				$map['name|tel|msg']=array("like","%".$keyword."%");
			}
			
			if($start){
				$map['addtime']=array('egt',$start);
			}
			if($end){
				$map['addtime']=array('elt',$end);
			}
			if($start&&$end){
				$map['addtime']=array('between',$start.",".$end);
			}

			$list=Db::name('msg_list')->where($map)->page($page.','.$limit)->order('msgid desc')->select();
				

			foreach ($list as $k => $v) {
				
				if($v['status'] =='1'){
					$list[$k]['statustype']="<span class='layui-badge-rim layui-bg-green'>已读</span>";
					
				}else if($v['status'] =='0'){
					$list[$k]['statustype']="<span class='layui-badge-rim layui-bg-gray'>未读</span>"; 
				}

			}


			$count=Db::name('msg_list')->where($map)->count('msgid');

			$arr=array(
				'code'=>"0",
				'count'=>$count,
				'data'=>$list
			);
			return json($arr);
	}
	#设为已读#
	public function setread(){

		$this->log("修改留言状态");

		$msgid=input("msgid");

		$status=input("status");

		$data=array(
			'msgid'=>$msgid,
			'status'=>$status,
			'uptime'=>date('Y-m-d H:i:s'),
		);
		echo ComModel::data_up('msg_list',$data);

	}


}